home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3241 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  113 lines

  1. Path: gryphon.phoenix.net!usenet
  2. From: brucew@phoenix.net (Bruce Wedding)
  3. Newsgroups: comp.lang.c,comp.lang.c++,alt.msdos.programmer
  4. Subject: Re: Clearing the keyboard buffer
  5. Date: Tue, 23 Jan 1996 00:43:22 GMT
  6. Organization: BranPaul Systems
  7. Message-ID: <4e18m5$on1@gryphon.phoenix.net>
  8. References: <4dtqi4$60u@cdc2.cdc.net>
  9. NNTP-Posting-Host: dial120.phoenix.net
  10. X-Newsreader: Moe's Newsreader    
  11.  
  12. mart@vianet.on.ca (Mart) wrote:
  13.  
  14. >  I'm using Turbo C++ v. 3.1 and I need to be able to clear the keyboard 
  15. >buffer. 
  16.  
  17. I'll probably be flamed for mentioning this, but I'm going to anyway.
  18. This is not STANDARD, in fact according to the ANSI Standard, it
  19. results in undefined behavior.  That ought to satisfy the pedants.
  20.  
  21. fflush(stdin); /* will flush the keyboard in Turbo C */
  22.  
  23. Here is another version from Snippets.  It includes a replacement
  24. kbhit that is 10s if not 100s of times faster than Borlands.
  25.  
  26. /*
  27. **  by David Goodenough & Bob Stout
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <dos.h>
  32. #include "extkword.h"
  33. #include "ext_keys.h"
  34. #include "mk_fp.h"
  35.  
  36. #define biosseg 0x40
  37.  
  38. #define HEAD (*((unsigned short FAR *)MK_FP(biosseg, 0x1a)))
  39. #define TAIL (*((unsigned short FAR *)MK_FP(biosseg, 0x1c)))
  40.  
  41. /*
  42. **  Detect a pending keypress
  43. */
  44.  
  45. int fast_kbhit(void)
  46. {
  47.       return HEAD - TAIL;
  48. }
  49.  
  50. /*
  51. **  Clear the keyboard buffer
  52. */
  53.  
  54. void fast_kbflush(void)
  55. {
  56.       HEAD = TAIL;
  57. }
  58.  
  59. /*
  60. **  Enhanced work-alike for BASIC's INKEY$ function
  61. */
  62.  
  63. int ext_inkey(void)
  64. {
  65.       if (!fast_kbhit())
  66.             return EOF;
  67.       else  return ext_getch();
  68. }
  69.  
  70. #ifdef TEST
  71.  
  72. #include <conio.h>
  73. #include <time.h>
  74.  
  75. main()
  76. {
  77.       clock_t Wait;
  78.       int key;
  79.  
  80.       puts("Hit some keys while I kill some time...");
  81.       Wait = clock();
  82.       while (2 > ((clock() - Wait) / CLK_TCK))
  83.             ;
  84.  
  85.       puts("OK, stop hitting keys while I flush the keyboard...");
  86.       Wait = clock();
  87.       while (2 > ((clock() - Wait) / CLK_TCK))
  88.             ;
  89.  
  90.       fast_kbflush();
  91.       puts("Optionally, hit some key you didn't hit before...");
  92.       Wait = clock();
  93.       while (2 > ((clock() - Wait) / CLK_TCK))
  94.             ;
  95.       if (EOF == (key = ext_inkey()))
  96.             puts("You didn't hit anything");
  97.       else  printf("You hit extended keycode 0x%04X\n", key);
  98.       
  99.       puts("OK, now hit some other key you didn't hit before...");
  100.  
  101.       while (!fast_kbhit())
  102.             ;
  103.       printf("You hit extended keycode 0x%04X\n", ext_getch());
  104.       return 0;
  105. }
  106.  
  107. #endif /* TEST */
  108.  
  109. Bruce D. Wedding                        Have Compiler, Will Travel!
  110.               Perspicacious Programming Performed Promptly
  111. Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
  112.  
  113.